import axios from 'axios'
import router from '@/router/index.js'
const service = axios.create({
  baseURL: 'http://localhost:8888/',
  timeout: 10000
})

// request interceptor
service.interceptors.request.use(
  (config) => {
    if (!config.url.includes('login')) {
      config.headers.token = JSON.parse(localStorage.getItem('employee')).empInfo.token
    }
    return config
  },
  (err) => {
    return Promise.reject(err)
  }
)

// response interceptor
service.interceptors.response.use(
  (res) => {
    if(res.data.code === 201) {
      router.push("/login")
      //还要清除用户信息
    }
    return res.data
  },
  (err) => {
    return Promise.reject(err)
  }
)
export default service
